package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectResultVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjUserLevel;
import com.zb.mapper.ZjUserLevelMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjUserLevelService;
import com.zb.service.ZjUserService;
import com.zb.vo.UserLevelListVo;
import com.zb.vo.UserLevelSelectVo;
import com.zb.vo.UserLevelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户等级表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-08
 */
@Service
public class ZjUserLevelServiceImpl extends ServiceImpl<ZjUserLevelMapper, ZjUserLevel> implements ZjUserLevelService {
    @Resource
    private ZjUserLevelMapper userLevelMapper;
    @Resource
    private ZjUserService userService;

    // 新增或修改用户级别
    @Override
    public void save(Long custAccId, UserLevelVo userLevelVo) {
        Assert.isTrue(null == userLevelMapper.existByName(custAccId, userLevelVo.getId(), userLevelVo.getName()), "级别名称已重复！", userLevelVo.getName());
        ZjUserLevel level = new ZjUserLevel();
        BeanUtils.copyProperties(userLevelVo, level);
        level.setDisabled(userLevelVo.getStatus());
        if (null != level.getId()) {
            this.updateById(level);
            return;
        }
        level.setId(IdWorker.getId());
        level.setCustAccId(custAccId);
        this.save(level);
    }

    // 获取用户级别列表
    @Override
    public Map<String, Object> getList(Long custAccId, UserLevelSelectVo selectVo) {
        QueryWrapper<ZjUserLevel> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.orderByDesc("gmt_update");
        if (null != selectVo.getStatus()) {
            wrapper.eq("is_disabled", selectVo.getStatus());
        }
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjUserLevel> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<UserLevelListVo> voList = new ArrayList<>();
        page.getRecords().forEach(t -> voList.add(UserLevelListVo.beanToVo(t)));
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 批量更改用户级别状态
    @Override
    public void updateBatchDisabledByIds(List<Long> idList, boolean status) {
        userLevelMapper.updateBatchDisabledByIds(idList, status);
    }

    // 批量删除用户级别状态
    @Override
    public void deleteBatchByIds(List<Long> idList) {
        List<String> levels = userService.existLevelTypeByUserLevelIds(idList);
        Assert.isTrue(null == levels || levels.isEmpty(), "用户级别[" + String.join("、", levels)+ "]已关联用户，不可删除！", null);
        this.removeByIds(idList);
    }

    // 分页获取用户级别下拉数据列表
    @Override
    public Map<String, Object> pageSelect(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjUserLevel> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name");
        wrapper.eq("cust_acc_id", custAccId)
                .eq("is_disabled", false)
                .orderByDesc("gmt_update");
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjUserLevel> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectResultVo> voList = new ArrayList<>();
        page.getRecords().forEach(t -> voList.add(new SelectResultVo(t.getId(), t.getName())));
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据级别ID获取级别名称
    @Override
    public String getNameById(Long id) {
        ZjUserLevel level = this.getById(id);
        return null == level? "" : level.getName();
    }
}
